package com.lms.filter;

import com.lms.bean.Root;
import com.lms.bean.User;
import com.lms.util.Constants;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SysFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest;
        HttpServletResponse httpServletResponse=(HttpServletResponse)servletResponse;

        User user = (User) httpServletRequest.getSession().getAttribute(Constants.USER_SESSION);
        Root root = (Root) httpServletRequest.getSession().getAttribute(Constants.ROOT_SESSION);
        if (user==null && root==null){
            httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/youShouldLogin.jsp");
        }else {
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
